from django.contrib.auth.models import Permission,Group
from django.contrib.contenttypes.models import ContentType
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from meiduo_admin.serializers.permissions import PermissionSerializer, ContentTypeSerialzier, GroupSerialzier, \
    PermissionSimpleSerializer, AdminSerializer
from users.models import User


#GET/meiduo_admin/permission/admins/
class AdminViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    #指定视图所使用的查询集
    queryset = User.objects.filter(is_staff=True)
    #指定序列化器类
    serializer_class = AdminSerializer

    def simple(self,request):
        """获取用户组数据"""
        #1.获取用户组数据
        groups = Group.objects.all()
        #2.将用户组数据序列化并返回
        serializer = GroupSerialzier(groups,many=True)
        return Response(serializer.data)



class GroupViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    # permission_classes = [IsAdminUser]

    #指定视图所使用的查询集
    queryset = Group.objects.all()
    #指定序列化器类
    serializer_class = GroupSerialzier

    def simple(self,request):
        """获取权限数据
        1.获取权限数据
        2。序列化并返回"""
        # 1.获取权限数据
        perms = Permission.objects.all()
        # 2.序列化并返回
        serialzier = PermissionSimpleSerializer(perms , many=True)
        return Response(serialzier.data)



#GET/meiduo_admin/permission/perms
class PermissionViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    #指定视图所使用的查询集
    queryset = Permission.objects.all()

    #指定视图所使用的序列化器类
    serializer_class = PermissionSerializer

    def content_types(self,request):
        """
        1.获取权限类型数据
        2.将权限数据序列化并返回
        """
        # 1.获取权限类型数据
        content_types = ContentType.objects.all()
        # 2.将权限数据序列化并返回
        serializer = ContentTypeSerialzier(content_types,many=True)
        return Response(serializer.data)
